package com.lanswon.qzsmk.util;


import java.util.concurrent.locks.ReentrantLock;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.lanswon.qzsmk.vo.BussinessVo;
import com.lanswon.qzsmk.vo.RechargeCancelVo;
import com.lanswon.smk.kmssclient.common.MacTacType;
import com.lanswon.smk.kmssclient.connection.ConnectionPool;
import com.lanswon.smk.kmssclient.connection.PoolVO;
import com.lanswon.smk.kmssclient.vo.KmssMac2ReqVO;
import com.lanswon.smk.kmssclient.vo.KmssTacReqVO;

/**
 * kmss工具
 * @author guyawei
 *
 */
public class ConnectionPoolUtil {
	
	private static Logger log = (Logger) LoggerFactory.getLogger(ConnectionPoolUtil.class);
	// 线程锁
    private static ReentrantLock lock = new ReentrantLock();

    private static ConnectionPoolUtil instance = null;
    
    public static ConnectionPool connPool = null;
    
    private static boolean usePool = false;// 默认不使用连接池
	
//    private static String host = KmssProperties.getInstance("KmssClient").getProperty("MidWareHost");
//    private static String port = KmssProperties.getInstance("KmssClient").getProperty("MidWarePort");
//    private static String incrementalConnections = KmssProperties.getInstance("KmssClient").getProperty("IncreNum");
//    private static String initialConnections = KmssProperties.getInstance("KmssClient").getProperty("InitNum");
//    private static String intervalTime = KmssProperties.getInstance("KmssClient").getProperty("IntervalTime");
//    private static String maxConnections = KmssProperties.getInstance("KmssClient").getProperty("MaxNum");
//    private static String overTime = KmssProperties.getInstance("KmssClient").getProperty("OverTime");
    
    private static PoolVO getPoolVO(){
		PoolVO vo = new PoolVO();
		vo.setHost(KmssProperties.getInstance("KmssClient").getProperty("MidWareHost"));
		vo.setPort(Integer.valueOf(KmssProperties.getInstance("KmssClient").getProperty("MidWarePort")));
		vo.setIncrementConns(Integer.valueOf(KmssProperties.getInstance("KmssClient").getProperty("IncreNum")));
		vo.setInitConns(Integer.valueOf(KmssProperties.getInstance("KmssClient").getProperty("InitNum")));
		vo.setIntervalTime(Integer.valueOf(KmssProperties.getInstance("KmssClient").getProperty("IntervalTime")));
		vo.setMaxConns(Integer.valueOf(KmssProperties.getInstance("KmssClient").getProperty("MaxNum")));
		vo.setOverTime(Integer.valueOf(KmssProperties.getInstance("KmssClient").getProperty("OverTime")));	
		return vo;
	}
	
    
	public  static ConnectionPoolUtil getInstance() throws Exception {
        try {
            if (instance == null) {
                lock.lock();
                if (instance == null) {
                    instance = new ConnectionPoolUtil();
                }
            }
        } catch (Exception ex) {
        	ex.printStackTrace();
            log.error("EncryptCheckUtil.getInstance()-失败", ex);
            throw ex;
        } finally {
            if (lock.isLocked()) {
            	lock.unlock();
            }
        }
        return instance;
    }
	
    public  ConnectionPoolUtil() throws Exception {
		instance = this;
        String use = KmssProperties.getInstance("KmssClient").getProperty("UsePool");
        usePool = "Y".equalsIgnoreCase(use);

        if (usePool) {
            log.debug(">>>使用连接池进行连接,初始化连接池>>>");
            connPool = new ConnectionPool(ConnectionPoolUtil.getPoolVO());
            connPool.createPool();
            log.debug(">>>连接池初始化完毕>>>");
        }
    }
	
//	public static ConnectionPool getConnectionPool(){
//		PoolVO vo = new PoolVO();
//		vo.setHost("192.168.0.12");
//		vo.setPort(9989);
//		vo.setIncrementConns(1);
//		vo.setInitConns(5);
//		vo.setIntervalTime(10);
//		vo.setMaxConns(10);
//		vo.setOverTime(2000);	
//		ConnectionPool connectionPool=new ConnectionPool(vo);
//		return connectionPool;
//	}

	/**获取mac1验证加密机对象**/
	public static  KmssMac2ReqVO getInstanceMac(BussinessVo bv){
		KmssMac2ReqVO resqVO=new KmssMac2ReqVO();
		resqVO.sethMoneyB4Load(bv.getCrdBalBefHex());
		resqVO.sethMoneyLoad(bv.getTxnAmtHex());
		resqVO.sethTradeType(bv.getCrdTxnType());
		resqVO.sethPsamNumber(bv.getSamNo());
		resqVO.setGens(bv.getDivFactor());
		resqVO.sethTradeTime(bv.getTxnDt());
		resqVO.sethAppType(bv.getAppType());
		resqVO.sethFakeRnd(bv.getRan());
		resqVO.sethTradeSeq(bv.getCurrCount());
		resqVO.sethMac1(bv.getMac1());
		return resqVO;
	
	}
	/**获取tac验证加密机对象**/
	public static  KmssTacReqVO getInstanceTac(BussinessVo bv){
		KmssTacReqVO  resqVO=new KmssTacReqVO ();
		resqVO.setiTacType(MacTacType.TAC_LOAD);
		resqVO.sethMoneyAfTrade(bv.getCrdBalAftHex());
		resqVO.sethCardTradeSeq(bv.getCurrCount());
		resqVO.sethAppType(bv.getAppType());
		resqVO.sethMoneyTrade(bv.getTxnAmtHex());
		resqVO.sethTradeType(bv.getCrdTxnType());
		resqVO.sethTradeTime(bv.getTxnDt());
		resqVO.sethPsamNumber(bv.getSamNo());
		resqVO.sethGens(bv.getDivFactor());
		return resqVO;
	
	}
	
	/**获取充值撤销tac验证加密机对象**/
	public static  KmssTacReqVO getInstanceTac(RechargeCancelVo rv){
		KmssTacReqVO  resqVO=new KmssTacReqVO ();
		resqVO.setiTacType(MacTacType.TAC_PURCHASE);
		resqVO.sethMoneyAfTrade(rv.getCrdBalAftHex());
		resqVO.sethCardTradeSeq(rv.getTradeSN());
		resqVO.sethAppType(rv.getAppType());
		resqVO.sethMoneyTrade(rv.getTxnAmtHex());
		resqVO.sethTradeType(rv.getCrdTxnType());
		resqVO.sethTradeTime(rv.getTxnDt());
		resqVO.sethPsamNumber(rv.getSamNo());
		resqVO.sethGens(rv.getDivFactor());
		resqVO.sethPosTradeSeq(rv.getSamCurrCount());
		return resqVO;
	
	}


}
